package com.itheima.controller;


import com.itheima.entity.Emp;
import com.itheima.entity.EmpQueryParam;
import com.itheima.entity.PageBean;
import com.itheima.entity.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;

    /**
     * 分页查询
     * @param
     * @param
     * @return
     */
    /*@GetMapping("/emps")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){

        PageBean pageBean = empService.page(page,pageSize);
        return Result.success(pageBean);
    }*/

    /*@GetMapping("/emps")
    public Result page(String name, Integer gender,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize){

        PageBean pageBean = empService.page(name,gender,begin,end,page,pageSize);
        return Result.success(pageBean);
    }*/

    @GetMapping("/emps")
    public Result page(EmpQueryParam param){

        PageBean pageBean = empService.page(param);
        return Result.success(pageBean);
    }


    /**
     * 新增
     * @param emp
     * @return
     */
    @PostMapping("/emps")
    public Result save(@RequestBody Emp emp){

        empService.save(emp);
        return Result.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/emps")
    public Result delete(@RequestParam List<Integer> ids){

        //1.调用Service层
        empService.delete(ids);
        return Result.success();
    }

    /**
     * 员工回显
     */
    @GetMapping("/emps/{id}")
    public Result getById(@PathVariable Integer id){
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    /**
     * 修改
     * @return
     */
    @PutMapping("/emps")
    public Result update(@RequestBody Emp emp){
        empService.update(emp);
        return Result.success();
    }


}
